package com.victor.controller.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.Manager;
import com.victor.service.ManagerService;
import com.victor.utils.Msg;

/**
 * 绘本馆后台登录和修改密码
 * 
 * @author zhangbin
 *
 */
@RestController
public class ManagerController {

	@Autowired
	private ManagerService managerService;

	// 管理员登录
	@RequestMapping(value = "/managerLogin")
	public Msg managerLogin(Manager manager) {
		if (managerService.loginexistmanager(manager.getAccount())) {// 判断账户是否存在
			Manager manager1 = managerService.loginManager(manager);// 登录
			if (manager1 != null) {
				return Msg.success().add("manager", manager1);
			}
			return Msg.fail("密码错误");
		}
		return Msg.fail("管理员账户不存在");
	}

	// 修改密码
	@RequestMapping(value = "/updManagerPassword")
	public Msg updManagerPassword(Manager manager) {
		if (manager == null || manager.getId() == 0) {
			return Msg.fail("参数错误");
		}

		Manager managerData = managerService.getManagerByid(manager.getId());
		if (managerData == null || managerData.getId() == 0) {
			return Msg.fail("参数错误");
		}

		// 通过id查询原密码
		if (manager.getPassword().equals(managerData.getPassword())) {
			return Msg.fail("新密码不可与原密码相同");
		}

		boolean updResult = managerService.updManagerPassword(manager);
		if (updResult) {
			return Msg.success();

		}
		return Msg.fail();
	}

}
